// pages/PersonalCenter/PersonalCenter.js
const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'个人中心',
    isNoMember: true,
    AssociatedCardShadow: true, //是否隐藏弹出窗
    cardNumber: '',
    password: '',
    userInfos: '',
    historyList: [], //收听历史
    collectList: [], //我的收藏
    followList: [], //我关注的人
    timer: null,
    isloading:false,
  },
  login() {
    app.ToLogin()
  },
  toMyComments() {
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      wx.navigateTo({
        url: '/pages/MyComments/MyComments'
      })
    }
  },
  toMyDownloads() {
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      wx.navigateTo({
        url: '/pages/MyDownloads/MyDownloads'
      })
    }
  },
  tosetting() {
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      wx.navigateTo({
        url: '/pages/setting/setting'
      })
    }
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    if (e.scrollTop > 20) {
      this.setData({
        defaultFix: true
      })
    } else {
      this.setData({
        defaultFix: false
      })
    }

  },
  // 防抖封装方法
  debounce(fun) {
    if (this.timer) {
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(() => {
      fun.call(this)
    }, 400)
  },
  // 关联卡号按钮
  AssociatedCardBtn() {
    this.setData({
      AssociatedCardShadow: false
    })
  },
  // 取消按钮
  cancelBtn() {
    this.setData({
      AssociatedCardShadow: true,
      cardNumber: '',
      password: ''
    })
  },
  // 确定按钮
  determineBtn() {
    console.log(Boolean(this.data.cardNumber))
    if (!Boolean(this.data.cardNumber) || !Boolean(this.data.password)) {
      wx.showToast({
        title: '请填写信息',
        icon: 'none'
      })
      return false
    }
    Controller.bindCard({
        cardNo: this.data.cardNumber,
        password: this.data.password
      })
      .then(res => {
        console.log('确定按钮', res)
        if (res.code == 0) {
          wx.showToast({
            title: res.msg
          })
          wx.setStorageSync('isNoMember', false)
          this.setData({
            AssociatedCardShadow: true,
            cardNumber: '',
            password: '',
            isNoMember: false
          })
          app.updateScore(2)
          this.getUserInfos()
        }
      }).catch(err => {
        console.log('err', err)
        wx.showToast({
          title: err.data.msg,
          icon: 'none'
        })
        this.setData({
          cardNumber: '',
          password: ''
        })
      })

  },
  // 请输入你的卡号/员工号
  cardNumberInput(e) {
    let val = e.detail.value;
    this.debounce(() => {
      this.setData({
        cardNumber: val
      })
    })
  },
  // 请输入密码
  passwordInput(e) {
    let val = e.detail.value;
    this.debounce(() => {
      this.setData({
        password: val
      })
    })
  },
  // 获取用户信息
  getUserInfos() {
    let that = this
    Controller.getUserInfo({
        token: wx.getStorageSync('logintoken')
      })
      .then(res => {
        console.log('获取用户信息', res)
        if (res.code == 0) {
          console.log(res)
          let userInfos = res.data.baseData
          // userInfos.listenTimes = (userInfos.listenTimes / 60 / 60).toFixed(3)
          that.setData({
            userInfos
          })
          wx.setStorageSync('userInfos', userInfos)
        }
        that.setData({
          isloading:true
        })
      }).catch(res=>{
        that.setData({
          isloading:true
        })
      })
  },
  // 收听历史
  getHistoryData() {
    let that = this
    Controller.historyList({
        page: 1,
        token: wx.getStorageSync('logintoken')
      })
      .then(res => {
        console.log('获取收听历史数据', res)
        if (res.code == 0) {
          let historyList = []
          res.data.baseData.list.map((item, index) => {
            if (index < 4) {
              historyList.push(item)
            }
          })
          that.setData({
            historyList
          })
        }
      })
  },
  // 获取收藏列表数据
  getCollectListData() {
    let that = this
    Controller.myCollectList({
        page: 1,
        token: wx.getStorageSync('logintoken')
      })
      .then(res => {
        console.log('获取收藏列表数据', res)

        if (res.code == 0) {
          let collectList = that.data.collectList;
          res.data.baseData.list.map((item, index) => {
            if (collectList.length < 4) {
              collectList.push(item)
            }
          })
          that.setData({
            collectList
          })
        }
      })
  },
  // 获取我关注的人列表数据
  getFollowList() {
    let that = this;
    Controller.followList({
        token: wx.getStorageSync('logintoken')
      })
      .then(res => {
        console.log('获取我关注的人列表数据', res)
        if (res.code == 0) {
          let followList = that.data.followList;
          // res.data.baseData.list.map((item, index) => {
          //   if (index < 4) {
          //     item.listenTimes = (item.listenTimes / 60 / 60).toFixed(2)
          //     followList.push(item)
          //   }
          // })
          that.setData({
            followList
          })
        }
      })
  },
  listenBtn(e) {
    console.log('听按钮', e)
    let {
      rid,
      type
    } = e.currentTarget.dataset
    let url = ''
    // 听按钮 1是栏目，2是合辑，3是音频，4是达人榜，5是评论
    if (type == 1) {
      url = "/pages/Exclusive/Exclusive?id=" + rid
    } else if (type == 2) {
      url = "/pages/Album/Detail/Detail?id=" + rid
    } else if (type == 3) {
      url = '/pages/Playing/Playing?audioId=' + rid
    } else if (type == 4) {
      url = '/pages/Talent/Other/Other?id=' + rid
    } else if (type == 5) {
      url = '/pages/Comment/Comment?rid=' + rid + "&type=" + type
    }
    wx.navigateTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    } else {
      this.setData({
        historyList: [],
        collectList: [],
        followList: []
      })
      this.getUserInfos()
      this.getHistoryData()
      this.getFollowList()
      this.getCollectListData()
      console.log('wx.getStorageSync("isNoMember")', wx.getStorageSync('isNoMember'))
      this.setData({
        isNoMember: wx.getStorageSync('isNoMember')
      })
    }
  },
  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·个人中心',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·个人中心',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})